package com.ccf.business.etl.model;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.common.utils.tool.StringUtil;
import com.common.log.LogFlowInfo;
import com.service.mybatis.pojo.SuperEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 
 *
 * @author ccf
 * @date 2021-12-07 18:19:02
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@TableName("wj_flow_log")
@ApiModel(value="wj_flow_log对象", description="流程日志")
public class FlowLog extends SuperEntity implements Delayed {
    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "日志id")
    private Long logId;

    @ApiModelProperty(value = "流程id")
    private String flowId;

    @ApiModelProperty(value = "流程名称")
    private String flowName;

    @ApiModelProperty(value = "节点id")
    private String nodeId;

    @ApiModelProperty(value = "节点名称")
    private String nodeName;

    @ApiModelProperty(value = "日志信息")
    private String msg;

    @ApiModelProperty(value = "日志级别")
    private String logLevel;

    @ApiModelProperty(value = "日志时间")
    private Date logTime;

    @ApiModelProperty(value = "延时时间")
    @TableField(exist = false)
    private long time;

    /**
     * 返回这个对象剩余的延时时间
     * 固定延迟0.5秒
     * @param unit
     * @return
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    /**
     * 控制队列中元素优先级
     * @param o
     * @return
     */
    @Override
    public int compareTo(Delayed o) {
        FlowLog item = (FlowLog) o;
        long diff = this.time - item.time;
        //表示第一个元素到期 所有元素都取出来
        if (diff >= 0) {
            return -1;
        }else {
            return 1;
        }
    }

    public FlowLog(LogFlowInfo logFlowInfo, long time) {
        super();
        this.logId = logFlowInfo.getId();
        this.flowId = logFlowInfo.getFlowId();
        this.flowName = logFlowInfo.getFlowName();
        this.nodeId = logFlowInfo.getNodeId();
        this.nodeName = logFlowInfo.getNodeName();
        this.msg = logFlowInfo.getMessage();
        this.logLevel = logFlowInfo.getLevel().name();
        this.logTime = new Date(logFlowInfo.getTime());
        this.time = System.currentTimeMillis() + time;
    }

    private static final String LOG_INFO_1 = "%s %s [%s:%s][%s:%s] : %s";
    private static final String LOG_INFO_2 = "%s %s [%s:%s] : %s";
    /**
     * 返回日志格式
     * @return
     */
    @Override
    public String toString(){
        String format = DateUtil.format(logTime, DatePattern.NORM_DATETIME_PATTERN);
        if (StrUtil.isNotBlank(nodeId)){
            return String.format(LOG_INFO_2, format, logLevel,  StringUtil.toString(nodeName), nodeId, msg);
        } else {
            return String.format(LOG_INFO_2, format, logLevel,  StringUtil.toString(flowName), flowId, msg);

        }
    }
}
